<template>
  <div class="save-as">
    <el-form :model="saveAsPropsStore" :inline="true">
      <el-form-item label="保存方式" style="width: 220px">
        <el-select
          v-model="saveAsPropsStore.saveMethod"
          @change="handleCheckSaveMethod"
        >
          <el-option label="覆盖原文件" value="0" />
          <el-option label="移动至" value="1" />
          <el-option label="另存至" value="2" />
        </el-select>
      </el-form-item>
      <template v-if="saveAsPropsStore.saveMethod !== '0'">
        <el-form-item>
          <div class="dir-select" @click="openFolder">
            <el-tooltip :content="saveAsPropsStore.targetDir">
              <div class="content nowrap">
                {{ saveAsPropsStore.targetDir }}
              </div>
            </el-tooltip>
            <el-tooltip content="修改输出目录">
              <i class="change-dir" @click.stop="changeFolder">
                <el-icon><MoreFilled /></el-icon>
              </i>
            </el-tooltip>
          </div>
          <el-tooltip content="打开文件夹">
            <i class="open-folder" @click="openFolder">
              <el-icon :size="24" color="#909399"><FolderOpened /></el-icon>
            </i>
          </el-tooltip>
        </el-form-item>
      </template>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import useSaveAsPropsStore from '@renderer/store/CpnProps/SaveAsPropsStore'
import { MoreFilled, FolderOpened } from '@element-plus/icons-vue'
import useGetSystemConfig from '@renderer/hooks/useGetSystemConfig'

const saveAsPropsStore = useSaveAsPropsStore()
const { getSaveTargetDir } = useGetSystemConfig()

const openFolder = async () => {
  const targetDir = saveAsPropsStore.targetDir
  if (targetDir) {
    await window.api.ensureDir(targetDir)
    await window.api.openPath(targetDir)
  }
}

// 更改目标目录
const changeFolder = async () => {
  const targetDir = await window.api.selectSingleFolder()
  if (targetDir) {
    localStorage.setItem('ele-target-dir', targetDir)
    saveAsPropsStore.targetDir = targetDir
  }
}

// 选择保存方式
const handleCheckSaveMethod = async () => {
  const { saveMethod, targetDir } = saveAsPropsStore
  if (['1', '2'].includes(saveMethod) && !targetDir) {
    saveAsPropsStore.targetDir = await getSaveTargetDir()
  }
}
</script>

<style lang="less" scoped>
.el-form-item {
  margin-right: 16px;
  margin-bottom: 0;
}

.save-as {
  display: flex;
  align-items: center;
  height: 60px;
  padding-left: 16px;
  color: var(--el-text-color-regular);

  .dir-select {
    display: flex;
    border: 1px solid var(--el-border-color);
    width: 200px;
    height: 30px;
    line-height: 30px;
    border-radius: 4px;
    cursor: pointer;

    .content {
      flex: 1;
      padding: 0 11px;
    }

    .change-dir {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 30px;
      font-size: 18px;
      border-left: 1px solid var(--el-border-color);
    }

    &:hover {
      border-color: var(--el-border-color-hover);

      .icon {
        color: var(--el-color-primary);
      }
    }
  }

  .open-folder {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-left: 14px;
    font-size: 24px;
    cursor: pointer;
  }
}
</style>
